﻿public class Solution {
    public int CanCompleteCircuit(int[] gas, int[] cost)
    {
        int total = 0; // 判断是否存在路径
        int start = 0; // 出发点
        int gasCost = 0; // 油耗情况
        for (int i = 0; i < gas.Length; i++)
        {
            gasCost += gas[i] - cost[i];
            total += gas[i] - cost[i];
            if (gasCost < 0)
            {
                start = i + 1;
                gasCost = 0;
            }
        }

        if (total < 0) return -1;
        return start;
    }

    public static void Main(string[] args)
    {
        int[] gas = [1, 2, 3, 4, 5], cost = [3, 4, 5, 1, 2];

        Solution solution = new Solution();
        int res = solution.CanCompleteCircuit(gas, cost);
        Console.WriteLine(res);
    }
}